<?php
/**
 * //w/w/w.s/h/u/o/t//u/p/u c/o/m  说/图/谱/源/码/分/享
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/25 0025
 * Time: 15:34
 */
defined("IN_IA")or exit("Access Denied");
checklogin();  //验证是否登录
global $_W,$_GPC;
$uniacid=$_W['uniacid'];
$op=$_GPC['op'] ? $_GPC['op'] : "shop_coupon";
//普通商城优惠券列表
if($op=='shop_coupon'){
    $all=pdo_getall('cqkundian_farm_shop_coupon',array('uniacid'=>$uniacid,'type'=>1,'is_delete'=>0));
    $count=count($all);
    $pageIndex=$_GPC['page'] ? $_GPC['page']:1;
    $pageSize=10;
    $pager=pagination($count,$pageIndex,$pageSize);
    $list=pdo_getall('cqkundian_farm_shop_coupon',array('uniacid'=>$uniacid,'type'=>1,'is_delete'=>0),'','','rank asc',array($pageIndex,$pageSize));
    include $this->template("web/coupon/shop_coupon_list");
}

//普通商城优惠券编辑
if($op=='shop_coupon_edit'){
    if(!empty($_GPC['id'])){
        $list=pdo_get('cqkundian_farm_shop_coupon',array('uniacid'=>$uniacid,'id'=>$_GPC['id']));
        $time['start']=date("Y-m-d",$list['begin_time']);
        $time['end']=date("Y-m-d",$list['end_time']);
    }
    include $this->template("web/coupon/shop_coupon_edit");
}

//普通商城优惠券保存
if($op=='shop_coupon_save'){
    $data=array(
        'coupon_name'=>$_GPC['coupon_name'],
        'coupon_type'=>$_GPC['coupon_type'],
        'low_cash_price'=>$_GPC['low_cash_price'],
        'coupon_price'=>$_GPC['coupon_price'],
        'expiry_date'=>$_GPC['expiry_date'],
        'expiry_day'=>$_GPC['expiry_day'],
        'status'=>$_GPC['status'],
        'rank'=>$_GPC['rank'],
        'count'=>$_GPC['count'],
        'uniacid'=>$uniacid,
        'type'=>1,  //普通商城可用
        'create_time'=>time(),
    );
    if($_GPC['expiry_date']==1){
        $data['expiry_day']=$_GPC['expiry_day'];
        $data['begin_time']=0;
        $data['end_time']=0;
    }else{
        $time=$_GPC['time'];
        $data['begin_time']=strtotime($time['start']);
        $data['end_time']=strtotime($time['end']);
        $data['expiry_day']=0;
    }
    if(empty($_GPC['id'])){
        $res=pdo_insert('cqkundian_farm_shop_coupon',$data);
    }else{
        $res=pdo_update('cqkundian_farm_shop_coupon',$data,array('id'=>$_GPC['id'],'uniacid'=>$uniacid));
    }
    if($res){
        message('操作成功',$this->createWebUrl('coupon'));
    }else{
        message('操作失败');
    }
    die;
}

//普通商城优惠券删除
if($op=='shop_coupon_delete'){
    $res=pdo_update('cqkundian_farm_shop_coupon',array('is_delete'=>1),array('uniacid'=>$uniacid,'id'=>$_GPC['id']));
//    pdo_delete('cqkundian_farm_user_coupon',array('uniacid'=>$uniacid,'cid'=>$_GPC['id']));
    if($res){
        echo json_encode(array('status'=>1));die;
    }else{
        echo json_encode(array('status'=>2));die;
    }
}


//组团商城优惠券列表
if($op=='group_coupon'){
    $all=pdo_getall('cqkundian_farm_shop_coupon',array('uniacid'=>$uniacid,'type'=>2,'is_delete'=>0));
    $count=count($all);
    $pageIndex=$_GPC['page'] ? $_GPC['page']:1;
    $pageSize=10;
    $pager=pagination($count,$pageIndex,$pageSize);
    $list=pdo_getall('cqkundian_farm_shop_coupon',array('uniacid'=>$uniacid,'type'=>2,'is_delete'=>0),'','','rank asc',array($pageIndex,$pageSize));
    include $this->template("web/coupon/group_coupon_list");
}

//组团商城优惠券编辑/新增
if($op=='group_coupon_edit'){
    if(!empty($_GPC['id'])){
        $list=pdo_get('cqkundian_farm_shop_coupon',array('uniacid'=>$uniacid,'id'=>$_GPC['id']));
        $time['start']=date("Y-m-d",$list['begin_time']);
        $time['end']=date("Y-m-d",$list['end_time']);
    }
    include $this->template("web/coupon/group_coupon_edit");
}

//组团商城优惠券保存
if($op=='group_coupon_save'){
    $data=array(
        'coupon_name'=>$_GPC['coupon_name'],
        'coupon_type'=>$_GPC['coupon_type'],
        'low_cash_price'=>$_GPC['low_cash_price'],
        'coupon_price'=>$_GPC['coupon_price'],
        'expiry_date'=>$_GPC['expiry_date'],
        'expiry_day'=>$_GPC['expiry_day'],
        'status'=>$_GPC['status'],
        'rank'=>$_GPC['rank'],
        'count'=>$_GPC['count'],
        'uniacid'=>$uniacid,
        'type'=>2,  //普通商城可用
        'create_time'=>time(),
    );
    if($_GPC['expiry_date']==1){
        $data['expiry_day']=$_GPC['expiry_day'];
        $data['begin_time']=0;
        $data['end_time']=0;
    }else{
        $time=$_GPC['time'];
        $data['begin_time']=strtotime($time['start']);
        $data['end_time']=strtotime($time['end']);
        $data['expiry_day']=0;
    }
    if(empty($_GPC['id'])){
        $res=pdo_insert('cqkundian_farm_shop_coupon',$data);
    }else{
        $res=pdo_update('cqkundian_farm_shop_coupon',$data,array('id'=>$_GPC['id'],'uniacid'=>$uniacid));
    }
    if($res){
        message('操作成功',url('site/entry/coupon',array('m'=>'kundian_farm','op'=>'group_coupon')));
    }else{
        message('操作失败');
    }
    die;
}

//畜牧优惠券
if($op=='adopt_coupon'){
    $all=pdo_getall('cqkundian_farm_shop_coupon',array('uniacid'=>$uniacid,'type'=>3,'is_delete'=>0));
    $count=count($all);
    $pageIndex=$_GPC['page'] ? $_GPC['page']:1;
    $pageSize=10;
    $pager=pagination($count,$pageIndex,$pageSize);
    $list=pdo_getall('cqkundian_farm_shop_coupon',array('uniacid'=>$uniacid,'type'=>3,'is_delete'=>0),'','','rank asc',array($pageIndex,$pageSize));
    include $this->template("web/coupon/adopt_coupon_list");
}

//畜牧优惠券新增/编辑
if($op=='adopt_coupon_edit'){
    if(!empty($_GPC['id'])){
        $list=pdo_get('cqkundian_farm_shop_coupon',array('uniacid'=>$uniacid,'id'=>$_GPC['id']));
        $time['start']=date("Y-m-d",$list['begin_time']);
        $time['end']=date("Y-m-d",$list['end_time']);
    }
    include $this->template("web/coupon/adopt_coupon_edit");
}

//畜牧优惠券保存
if($op=='adopt_coupon_save'){
    $data=array(
        'coupon_name'=>$_GPC['coupon_name'],
        'coupon_type'=>$_GPC['coupon_type'],
        'low_cash_price'=>$_GPC['low_cash_price'],
        'coupon_price'=>$_GPC['coupon_price'],
        'expiry_date'=>$_GPC['expiry_date'],
        'expiry_day'=>$_GPC['expiry_day'],
        'status'=>$_GPC['status'],
        'rank'=>$_GPC['rank'],
        'count'=>$_GPC['count'],
        'uniacid'=>$uniacid,
        'type'=>3,  //普通商城可用
        'create_time'=>time(),
    );
    if($_GPC['expiry_date']==1){
        $data['expiry_day']=$_GPC['expiry_day'];
        $data['begin_time']=0;
        $data['end_time']=0;
    }else{
        $time=$_GPC['time'];
        $data['begin_time']=strtotime($time['start']);
        $data['end_time']=strtotime($time['end']);
        $data['expiry_day']=0;
    }
    if(empty($_GPC['id'])){
        $res=pdo_insert('cqkundian_farm_shop_coupon',$data);
    }else{
        $res=pdo_update('cqkundian_farm_shop_coupon',$data,array('id'=>$_GPC['id'],'uniacid'=>$uniacid));
    }
    if($res){
        message('操作成功',url('site/entry/coupon',array('m'=>'kundian_farm','op'=>'adopt_coupon')));
    }else{
        message('操作失败');
    }
    die;
}


//土地优惠券
if($op=='land_coupon'){
    $all=pdo_getall('cqkundian_farm_shop_coupon',array('uniacid'=>$uniacid,'type'=>4,'is_delete'=>0));
    $count=count($all);
    $pageIndex=$_GPC['page'] ? $_GPC['page']:1;
    $pageSize=10;
    $pager=pagination($count,$pageIndex,$pageSize);
    $list=pdo_getall('cqkundian_farm_shop_coupon',array('uniacid'=>$uniacid,'type'=>4,'is_delete'=>0),'','','rank asc',array($pageIndex,$pageSize));
    include $this->template("web/coupon/land_coupon_list");
}

//土地优惠券新增/编辑
if($op=='land_coupon_edit'){
    if(!empty($_GPC['id'])){
        $list=pdo_get('cqkundian_farm_shop_coupon',array('uniacid'=>$uniacid,'id'=>$_GPC['id']));
        $time['start']=date("Y-m-d",$list['begin_time']);
        $time['end']=date("Y-m-d",$list['end_time']);
    }
    include $this->template("web/coupon/land_coupon_edit");
}

//土地优惠券保存
if($op=='land_coupon_save'){
    $data=array(
        'coupon_name'=>$_GPC['coupon_name'],
        'coupon_type'=>$_GPC['coupon_type'],
        'low_cash_price'=>$_GPC['low_cash_price'],
        'coupon_price'=>$_GPC['coupon_price'],
        'expiry_date'=>$_GPC['expiry_date'],
        'expiry_day'=>$_GPC['expiry_day'],
        'status'=>$_GPC['status'],
        'rank'=>$_GPC['rank'],
        'count'=>$_GPC['count'],
        'uniacid'=>$uniacid,
        'type'=>4,  //普通商城可用
        'create_time'=>time(),
    );
    if($_GPC['expiry_date']==1){
        $data['expiry_day']=$_GPC['expiry_day'];
        $data['begin_time']=0;
        $data['end_time']=0;
    }else{
        $time=$_GPC['time'];
        $data['begin_time']=strtotime($time['start']);
        $data['end_time']=strtotime($time['end']);
        $data['expiry_day']=0;
    }
    if(empty($_GPC['id'])){
        $res=pdo_insert('cqkundian_farm_shop_coupon',$data);
    }else{
        $res=pdo_update('cqkundian_farm_shop_coupon',$data,array('id'=>$_GPC['id'],'uniacid'=>$uniacid));
    }
    if($res){
        message('操作成功',url('site/entry/coupon',array('m'=>'kundian_farm','op'=>'land_coupon')));
    }else{
        message('操作失败');
    }
    die;
}
//种子优惠券
if($op=='seed_coupon'){
    $all=pdo_getall('cqkundian_farm_shop_coupon',array('uniacid'=>$uniacid,'type'=>5,'is_delete'=>0));
    $count=count($all);
    $pageIndex=$_GPC['page'] ? $_GPC['page']:1;
    $pageSize=10;
    $pager=pagination($count,$pageIndex,$pageSize);
    $list=pdo_getall('cqkundian_farm_shop_coupon',array('uniacid'=>$uniacid,'type'=>5,'is_delete'=>0),'','','rank asc',array($pageIndex,$pageSize));
    include $this->template("web/coupon/seed_coupon_list");
}

//种子优惠券新增/编辑
if($op=='seed_coupon_edit'){
    if(!empty($_GPC['id'])){
        $list=pdo_get('cqkundian_farm_shop_coupon',array('uniacid'=>$uniacid,'id'=>$_GPC['id']));
        $time['start']=date("Y-m-d",$list['begin_time']);
        $time['end']=date("Y-m-d",$list['end_time']);
    }
    include $this->template("web/coupon/seed_coupon_edit");
}

//种子优惠券保存
if($op=='seed_coupon_save'){
    $data=array(
        'coupon_name'=>$_GPC['coupon_name'],
        'coupon_type'=>$_GPC['coupon_type'],
        'low_cash_price'=>$_GPC['low_cash_price'],
        'coupon_price'=>$_GPC['coupon_price'],
        'expiry_date'=>$_GPC['expiry_date'],
        'expiry_day'=>$_GPC['expiry_day'],
        'status'=>$_GPC['status'],
        'rank'=>$_GPC['rank'],
        'count'=>$_GPC['count'],
        'uniacid'=>$uniacid,
        'type'=>5,  //普通商城可用
        'create_time'=>time(),
    );
    if($_GPC['expiry_date']==1){
        $data['expiry_day']=$_GPC['expiry_day'];
        $data['begin_time']=0;
        $data['end_time']=0;
    }else{
        $time=$_GPC['time'];
        $data['begin_time']=strtotime($time['start']);
        $data['end_time']=strtotime($time['end']);
        $data['expiry_day']=0;
    }
    if(empty($_GPC['id'])){
        $res=pdo_insert('cqkundian_farm_shop_coupon',$data);
    }else{
        $res=pdo_update('cqkundian_farm_shop_coupon',$data,array('id'=>$_GPC['id'],'uniacid'=>$uniacid));
    }
    if($res){
        message('操作成功',url('site/entry/coupon',array('m'=>'kundian_farm','op'=>'seed_coupon')));
    }else{
        message('操作失败');
    }
    die;
}

//发放优惠券
if($op=='grant_coupon'){
    $id=$_GPC['id'];
    $list=pdo_get('cqkundian_farm_shop_coupon',array('uniacid'=>$uniacid,'id'=>$_GPC['id']));
    $list['low_cash_price']=number_format($list['low_cash_price'],2);
    $list['coupon_price']=number_format($list['coupon_price'],2);
    include $this->template("web/coupon/grant_coupon");
}

//搜索用户信息
if($op=='searchUser'){
    $nickname=$_GPC['nickname'];
    $condition['nickname LIKE']='%'.$nickname.'%';
    $condition['uniacid']=$uniacid;
    $userData=pdo_getall('cqkundian_farm_user',$condition);
    echo json_encode(array('userData'=>$userData));die;
}

//保存发放优惠券
if($op=='grant_coupon_save'){
    $id=$_GPC['id'];
    $user_id_list=$_GPC['user_id_list'];
    $res=0;
    for ($i=0;$i<count($user_id_list);$i++){
        $data=array(
            'cid'=>$id,
            'uid'=>$user_id_list[$i],
            'uniacid'=>$uniacid,
            'create_time'=>time(),
        );
        $res+=pdo_insert('cqkundian_farm_user_coupon',$data);
    }
    if($res){
        message('操作成功',url("site/entry/coupon",array('m'=>'kundian_farm','op'=>'shop_coupon')));
    }else{
        message('操作失败');
    }
}






